Skip to content

feat(Telemetry): Emit CLI usage events from an API middleware - #8541

Open
ahmadjafari86 wants to merge 2 commits into
Flagsmith:mainfrom
ahmadjafari86:feat/8319-cli-usage-middleware
Open

ahmadjafari86 wants to merge 2 commits into
Flagsmith:mainfrom
ahmadjafari86:feat/8319-cli-usage-middleware

Conversation

@ahmadjafari86

Copy link
Copy Markdown

Thanks for submitting a PR! Please check the boxes below:

  • I have read the [Contributing Guide](/Flagsmith/flagsmith/blob/main/CONTRIBUTING.md).
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Closes #8319

Flagsmith CLI requests currently don't emit dedicated usage telemetry. This adds middleware to record authenticated CLI usage so it can be observed consistently without introducing database or cache reads in the telemetry path.

  • Emits cli.request.made for authenticated CLI requests.
  • Captures CLI version, OS, architecture, organisation, request status, and user identity where applicable.
  • Supports both authenticated users and Master API Keys.
  • Shares organisation resolution logic with the existing MCP telemetry middleware.

Review effort: 2/5

How did you test this code?

Added unit tests covering authenticated and unauthenticated requests, Master API Keys, error responses, CLI version passthrough, non-CLI User-Agents, and zero database queries during CLI telemetry handling.

Also verified with:

make test opts='tests/unit/telemetry/test_unit_telemetry_middleware.py -n0'
make lint
make typecheck
git diff --check

@vercel

vercel Bot commented Sep 16, 2026

Copy link
Copy Markdown

@ahmadjafari86 is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Sep 16, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 3e4985cf-3bd5-4297-ad13-b8faa9dad64f

📥 Commits

Reviewing files that changed from the base of the PR and between 81103d4 and a9f1beb.

📒 Files selected for processing (2)
  • api/projects/views.py
  • api/tests/unit/telemetry/test_unit_telemetry_middleware.py

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The API adds and registers CLIUsageLoggerMiddleware. Authenticated requests with a matching flagsmith-cli user agent emit request.made telemetry with CLI details, status, organisation ID, and, where applicable, the Amplitude user ID. Organisation resolution is shared with MCP telemetry. Project listing records the organisation span attribute when applicable. Tests cover request variants, status handling, version parsing, and database queries. The event catalogue updates MCP source locations.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added api Issue related to the REST API docs Documentation updates labels Sep 16, 2026
@ahmadjafari86
ahmadjafari86 marked this pull request as ready for review September 19, 2026 19:34
@ahmadjafari86
ahmadjafari86 requested review from a team as code owners September 19, 2026 19:34
@ahmadjafari86
ahmadjafari86 requested review from matthewelwell and removed request for a team September 19, 2026 19:34

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 9a2f8873-0c9a-48ca-8fb5-d296c575e914

📥 Commits

Reviewing files that changed from the base of the PR and between 7efa778 and 81103d4.

📒 Files selected for processing (4)
  • api/app/settings/common.py
  • api/telemetry/middleware.py
  • api/tests/unit/telemetry/test_unit_telemetry_middleware.py
  • docs/docs/deployment-self-hosting/observability/_events-catalogue.md

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment thread api/telemetry/middleware.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API docs Documentation updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emit CLI usage events from an API middleware

1 participant